package cn.ocean.org.service.impl;

import cn.ocean.basic.service.impl.BaseServiceImpl;
import cn.ocean.basic.utils.PageList;
import cn.ocean.org.domain.Department;
import cn.ocean.org.mapper.DepartmentMapper;
import cn.ocean.org.query.DepartmentQuery;
import cn.ocean.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* @Description: servics实现类
* @Author: Fill
* @Version: 1.0
* @Date:  19:33 19:33
*/

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {


    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void updateDept(Department department) {
        String dirPhat = "";
        if (department.getParent().getId() == null){
            dirPhat = "/" +department.getId();
        } else {
            Department parentDept = departmentMapper.getByIdDept(department.getParent().getId());
            if (parentDept != null){
                dirPhat = parentDept.getDirPath() + "/"+ department.getId();
            }

        }
        department.setDirPath(dirPhat);
        departmentMapper.updateDept(department);

    }

    /**
     * @Title: 增加
     * @Description: TODO
     * @Author: Fill
     * @Version: 1.0
     * @Date: 19:30 19:30
     * @Parameters: [department]
     * @Return void
     */
    @Override
    public void addDept(Department department) {
        departmentMapper.addDept(department);
        String dirPhat = "";
        if (department.getParent().getId() == null){
            dirPhat = "/" +department.getId();
        } else {
            Department parentDept = departmentMapper.getByIdDept(department.getParent().getId());
            if (parentDept != null){
                dirPhat = parentDept.getDirPath() + "/"+ department.getId();
            }

        }
        department.setDirPath(dirPhat);
        departmentMapper.updateDept(department);

    }





    /**
    * @Title: tree
    * @Description: 部门数据树
    * @Author: Fill
    * @Version: 1.0
    * @Date:  2022/6/26 10:07
    * @Parameters: []
    * @Return java.util.List<cn.ocean.org.domain.Department>
    */
  /*  @Override
    public List<Department> tree() {
        // 获取部门表的所有数据
        List<Department> departments = departmentMapper.loadAllDept();
        // 创建一个新的集合，用来存放数据
        ArrayList<Department> arrayList = new ArrayList<>();
        // 遍历集合获取其中的数据
        for (Department deptTmp : departments) {
            // 判断上级部门id是否为空，为空则为一级部门
            if (deptTmp.getParent_id() == null) {
                // 将数据添加进arrayList
                arrayList.add(deptTmp);
            } else {
                // 第二重循环，寻找不为空的父级
                for (Department parent : departments) {
                    // 判断上级部门id是否与主键id一致来找父级，若相等则为父级
                    if (parent.getId() == deptTmp.getParent_id()){
                        // 将数据添加进子集集合中
                        parent.getChildren().add(deptTmp);
                        break;

                    }
                }
            }
        }

        return arrayList;
    }*/
    @Override
    public List<Department> tree() {
        // 获取部门表的所有数据
        List<Department> departments = departmentMapper.loadAllDept();
        // 创建一个新的集合，用来存放数据
        ArrayList<Department> arrayList = new ArrayList<>();
        HashMap<Long, Department> hashMap = new HashMap<>();
        for (Department department:departments) {
            hashMap.put(department.getId(),department);
        }
        for (Department deptTmp: departments ) {
            if (deptTmp.getParent_id()==null){
                arrayList.add(deptTmp);

            }else {
                Department department = hashMap.get(deptTmp.getParent_id());
                if (department !=null){
                    department.getChildren().add(deptTmp);
                }
            }
        }
        return arrayList;
    }
}